package com.hongyuan.sellfitness.ui.person.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivityUserInfoBinding;
import com.hongyuan.sellfitness.my_view.StickyScrollView;
import com.hongyuan.sellfitness.ui.person.view_mode.UserInfoViewModel;
import com.hongyuan.sellfitness.utils.StatusBarUtil;

import me.tatarka.bindingcollectionadapter2.BR;

public class UserInfoActivity extends CustomActivity<ActivityUserInfoBinding, UserInfoViewModel> implements StickyScrollView.ScrollViewListener{

    private String userId;
    private String userPhone;

    //渐变高度
    private int height;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_user_info;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserInfoViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(UserInfoViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();

        userId = getBundle().getString("user_id");
        userPhone = getBundle().getString("userPhone");
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);

        if(viewModel.userToken.getM_mobile().equals(userPhone)){
            setEnableLoadMore(Constants.GET_MEMBER_CIRCLE_LIST);
            setEnableRefresh(Constants.GET_MEMBER_CIRCLE_LIST);
        }else{
            setEnableLoadMore(Constants.GET_OTHER_MBEMBER_CIRCLE_LIST);
            setEnableRefresh(Constants.GET_OTHER_MBEMBER_CIRCLE_LIST);
        }

        binding.comBack.setOnClickListener(view -> finish());
        StatusBarUtil.setCommonUI(this,false);

        binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
        initListeners();

    }

    /*
     * 获取头部图片的高度
     * */
    private void initListeners() {
        ViewTreeObserver vto = binding.bgImg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.titleBox.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = binding.bgImg.getHeight();

                binding.mScroll.setScrollViewListener(UserInfoActivity.this);
            }
        });
    }

    @Override
    public void onScrollChanged(StickyScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            binding.myTitle.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
            binding.coachTv.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
            binding.comBack.setImageResource(R.mipmap.white_common);
            binding.titleBox.setBackgroundColor(Color.argb((int) 0, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
        } else if (y > 0 && y <= height) {
            float scale = (float) y / height;
            float alpha = (255 * scale);

            binding.titleBox.setBackgroundColor(Color.argb((int) alpha, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) alpha, 238,238,238));
            if(y <= height/2){
                binding.myTitle.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
                binding.coachTv.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
                binding.comBack.setImageResource(R.mipmap.white_common);
                StatusBarUtil.setCommonUI(this,false);
            }else{
                binding.myTitle.setTextColor(getResources().getColor(R.color.theme_333333));
                binding.coachTv.setTextColor(getResources().getColor(R.color.theme_333333));
                binding.comBack.setImageResource(R.mipmap.back_common);
                StatusBarUtil.setCommonUI(this,true);
            }
        } else {
            binding.myTitle.setTextColor(getResources().getColor(R.color.theme_333333));
            binding.coachTv.setTextColor(getResources().getColor(R.color.theme_333333));
            binding.comBack.setImageResource(R.mipmap.back_common);
            binding.titleBox.setBackgroundColor(Color.argb((int) 255, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 255, 238,238,238));
            StatusBarUtil.setCommonUI(this,true);
        }
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.getUserInfo(userId,userPhone);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.changeTitle.observe(this, s -> {
            binding.myTitle.setText(s);
        });
        viewModel.uc.changeButtonTv.observe(this,s -> {
            binding.buttonTv.setText(s);
        });
    }
}
